package com.jhclothing.web.controller.product;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.service.IFabricService;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.common.core.page.TableDataInfo;

/**
 * 布料Controller
 *
 * @author zyp
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/product/fabric")
public class FabricController extends BaseController {
    @Autowired
    private IFabricService fabricService;

    /**
     * 查询布料列表
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:list')")
    @GetMapping("/list")
    public TableDataInfo list(Fabric fabric) {
        startPage();
        List<Fabric> list = fabricService.selectFabricList(fabric);
        return getDataTable(list);
    }

    /**
     * 导出布料列表
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:export')")
    @Log(title = "布料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Fabric fabric) {
        List<Fabric> list = fabricService.selectFabricList(fabric);
        ExcelUtil<Fabric> util = new ExcelUtil<>(Fabric.class);
        util.exportExcel(response, list, "布料信息");
    }

    /**
     * 获取布料详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:query')")
    @GetMapping(value = "/{fabricId}")
    public AjaxResult getInfo(@PathVariable("fabricId") Long fabricId) {
        return success(fabricService.selectFabricByFabricId(fabricId));
    }

    /**
     * 新增布料
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:add')")
    @Log(title = "布料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Fabric fabric) {
        if (!fabricService.checkFabricCodeUnique(fabric)) {
            return error("新增布料信息'" + fabric.getFabricCode() + "'失败，布料编码已存在");
        }
        fabric.setCreateBy(getUsername());
        return toAjax(fabricService.insertFabric(fabric));
    }

    /**
     * 修改布料
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:edit')")
    @Log(title = "布料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Fabric fabric) {
        if (!fabricService.checkFabricCodeUnique(fabric)) {
            return error("修改布料信息'" + fabric.getFabricCode() + "'失败，布料编码已存在");
        }
        return toAjax(fabricService.updateFabric(fabric));
    }

    /**
     * 删除布料
     */
    @PreAuthorize("@ss.hasPermi('product:fabric:remove')")
    @Log(title = "布料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fabricIds}")
    public AjaxResult remove(@PathVariable Long[] fabricIds) {
        return toAjax(fabricService.deleteFabricByFabricIds(fabricIds));
    }
}
